"""
    indexurl="https://www.17qcc.com/list.html?cateId=50010158&spm=1.1.4-20.22&page=1"
    发送indexurl获取qccppm字段，进行分割，key,iv,time
    url="https://newopenapiweb.17qcc.com/api/services/app/SearchFactory/GetPageList"
    发送url获取数据
"""

import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding="utf-8")
import re
import json
import execjs
import requests


class Qcw:
    def __init__(self):
        self.requests = requests.session()
        self.header = {
            "accept": "application/json, text/javascript, */*; q=0.01",
            "accept-language": "zh-CN,zh;q=0.9",
            "app-guestid": "19673584A8B0B539634CD41F786D1244",
            "app-version": "0",
            "cache-control": "no-cache",
            "content-type": "application/json",
            "origin": "https://www.17qcc.com",
            "pragma": "no-cache",
            "priority": "u=1, i",
            "referer": "https://www.17qcc.com/",
            "sec-ch-ua": "\"Microsoft Edge\";v=\"135\", \"Not-A.Brand\";v=\"8\", \"Chromium\";v=\"135\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-site",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0"
        }
        self.indexUrl = "https://hangzhou.qccqcc.com/"
        self.url = "https://newopenapiweb.17qcc.com/api/services/app/SearchFactory/GetPageList"
        self.js = execjs.compile(open('杭州服饰.js', 'r', encoding='utf-8').read())
        self.key = ''
        self.iv = ''
        self.time = ''

    def getIndexUrl(self):
        try:
            response = self.requests.get(url=self.indexUrl, headers=self.header)
            response.raise_for_status()
            response.encoding = response.apparent_encoding
            data = re.findall('var qccppm = "(.*?)";', response.text)[0].split('|')
            self.key = data[0]
            self.iv = data[1]
            self.time = data[2]
        except Exception as e:
            print(e)

    # 获取数据
    def get_data(self, page):
        code = self.js.call('get_data', page, self.key, self.iv)
        data = {
            "Content": code["Content"],
            "Sign": code["Sign"],
            "RsaPubAes": code["RsaPubAes"],
            "IV": self.iv,
            "TimesTamp": self.time
        }
        data = json.dumps(data, separators=(',', ':'))
        try:
            response = self.requests.post(url=self.url, headers=self.header, data=data)
            response.raise_for_status()
            return response.json()['Result']
        except Exception as e:
            print(e)

    def parse_data(self, data):
        res = self.js.call('_0x316c29', data, self.key, self.iv)
        print(type(res))
        print(res)

    def mian(self):
        self.getIndexUrl()
        for page in range(2, 3):
            response = self.get_data(page)
            self.parse_data(response)


if __name__ == '__main__':
    qcw = Qcw()
    qcw.mian()
